'''
Description: For processing data and ploting curves
Author: ZiLu
Date: 2021-06-20 14:32:28
Version: 
LastEditTime: 2021-06-21 11:41:08
LastEditors: ZiLu
'''

import matplotlib.pyplot as plt
import numpy as np
from math import log10
import scipy.optimize as optimize
from openpyxl import load_workbook 

#coding:utf-8
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号

data_path = "RSSI_data.xlsx"
wb = load_workbook(filename=data_path, data_only=True)
sheet = wb["Sheet1"]

def get_value():
    r = []
    for cell in sheet["A4":"A30"]:
        r.append(cell[0].value)
    for cell in sheet["E4":"E30"]:
        r.append(cell[0].value)

    pr_dbm_indoors = []
    for cell in sheet["C4":"C30"]:
        pr_dbm_indoors.append(cell[0].value)
    for cell in sheet["G4":"G30"]:
        pr_dbm_indoors.append(cell[0].value)

    pr_mw_indoors = []
    for cell in sheet["D4":"D30"]:
        pr_mw_indoors.append(cell[0].value)
    for cell in sheet["H4":"H30"]:
        pr_mw_indoors.append(cell[0].value)

    pr_dbm_outdoors = []
    for cell in sheet["C37":"C63"]:
        pr_dbm_outdoors.append(cell[0].value)
    for cell in sheet["G37":"G63"]:
        pr_dbm_outdoors.append(cell[0].value)

    pr_mw_outdoors = []
    for cell in sheet["D37":"D63"]:
        pr_mw_outdoors.append(cell[0].value)
    for cell in sheet["H37":"H63"]:
        pr_mw_outdoors.append(cell[0].value)

    return np.array(r), np.array(pr_dbm_indoors), np.array(pr_mw_indoors), np.array(pr_dbm_outdoors), np.array(pr_mw_outdoors)

# PR(dBm) = A-10・nlgr, where A=10lgPT
def pr_dbm_func(r, r0, A, n):
    return A - n * np.log10((r + r0) / 100 + np.spacing(1))

def pr_mw_func(r, r0, PT, n):
    return (PT / pow((r + r0) / 100, n)) * 1e-6

def show(r, pr, func, title, unit):
    para, cur = optimize.curve_fit(func, r, pr, maxfev=50000)
    pr_fit = [func(x, *para) for x in r]
    print(para)

    plt.scatter(r + para[0], pr, marker='^', color="#0097a7", label="raw data")
    plt.xlabel("Distance/cm")
    plt.ylabel("PR/" + unit)
    plt.title(title)
    plt.plot(r + para[0], pr_fit, color="orange", label="fitted curve")
    # plt.text("发射功率PT/dBm:" + str(para[1]))
    # plt.text("距离矫正r0/cm:" + str(para[0]))
    # plt.text("传播因子PT/dBm:"+str(para[2]))
    plt.legend()
    plt.grid()
    plt.show()

r, pr_dbm_indoors, pr_mw_indoors, pr_dbm_outdoors, pr_mw_outdoors = get_value()
r_slash = r[0:-10]
pr_dbm_indoors_slash = pr_dbm_indoors[0:-10]
pr_mw_indoors_slash = pr_mw_indoors[0:-10]
pr_dbm_outdoors_slash = pr_dbm_outdoors[0:-10]
pr_mw_outdoors_slash = pr_mw_outdoors[0:-10]

show(r, pr_dbm_indoors, pr_dbm_func, "Indoors Power Receive Figure(using data within 200cm)", "dBm")
show(r_slash, pr_dbm_indoors_slash, pr_dbm_func, "Indoors Power Receive Figure(using data within 100cm)", "dBm")

show(r, pr_dbm_outdoors, pr_dbm_func, "Outdoors Power Receive Figure(using data within 200cm)", "dBm")
show(r_slash, pr_dbm_outdoors_slash, pr_dbm_func, "Outdoors Power Receive Figure(using data within 100cm)", "dBm")

# maybe inaccuracy(sharp variance)
# show(r, pr_mw_indoors, pr_mw_func, "Indoors Power Receive Figure(using data within 200cm)", "*1e-6 mw")
# show(r_slash, pr_mw_indoors_slash, pr_mw_func, "Indoors Power Receive Figure(using data within 100cm)", "*1e-6 mw")

# show(r, pr_mw_outdoors, pr_mw_func, "Outdoors Power Receive Figure(using data within 200cm)", "*1e-6 mw")
# show(r_slash, pr_mw_outdoors_slash, pr_mw_func, "Outdoors Power Receive Figure(using data within 100cm)", "*1e-6 mw")

print("hhh")